home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / world / motion / motion.java < prev    next >
Text File  |  1996-10-17  |  2KB  |  167 lines

  1. // "Motion Shadow Effect"
  2.  
  3. //     created by ask@krc.sony.co.jp (Masamichi zzzcat Asukai)
  4.  
  5. //
  6.  
  7. // Copyright(C) 1996 Sony Corporation. All rights reserved.
  8.  
  9. //
  10.  
  11.  
  12.  
  13. import vrml.*;
  14.  
  15. import vrml.node.*;
  16.  
  17. import vrml.field.*;
  18.  
  19.  
  20.  
  21. public class motion extends Script {
  22.  
  23.  
  24.  
  25.     private SFRotation setRotation;
  26.  
  27.     private SFVec3f setScale;
  28.  
  29.  
  30.  
  31.     private Node tree;
  32.  
  33.     private Node light;
  34.  
  35.  
  36.  
  37.     private float[] s_rot = {0.0f, 1.0f, 0.0f, 0.0f};
  38.  
  39.     private float[] s_scale = {1.0f, 1.0f, 1.0f};
  40.  
  41.  
  42.  
  43.     private float[] t_trans = new float[3];
  44.  
  45.     private float[] t_scale = new float[3];
  46.  
  47.     private float[] l_trans = new float[3];
  48.  
  49.  
  50.  
  51.     private float[] vec = new float[3];
  52.  
  53.     private float d;
  54.  
  55.  
  56.  
  57.     // constructor
  58.  
  59.     public void initialize() {
  60.  
  61.     setRotation = (SFRotation)getEventOut("setRotation");
  62.  
  63.     setScale = (SFVec3f)getEventOut("setScale");
  64.  
  65.  
  66.  
  67.     tree = (Node)((SFNode)getField("tree")).getValue();
  68.  
  69.     light = (Node)((SFNode)getField("light")).getValue();
  70.  
  71.  
  72.  
  73.     // get translation and scale of tree
  74.  
  75.     ((SFVec3f)tree.getExposedField("translation")).getValue(t_trans);
  76.  
  77.     ((SFVec3f)tree.getExposedField("scale")).getValue(t_scale);
  78.  
  79.     }
  80.  
  81.     
  82.  
  83.     public void processEvent(Event e) {
  84.  
  85.     if (e.getName().equals("interval")) {
  86.  
  87.         // get translation of light
  88.  
  89.         ((SFVec3f)light.getExposedField("translation")).getValue(l_trans);
  90.  
  91.  
  92.  
  93.         ////////////////////
  94.  
  95.         // SHADOW ROTATION
  96.  
  97.         ////////////////////
  98.  
  99.  
  100.  
  101.         // normalized light vector on X-Z plane
  102.  
  103.         vec[0] = t_trans[0] - l_trans[0];
  104.  
  105.         vec[2] = t_trans[2] - l_trans[2];
  106.  
  107.         d = (float)java.lang.Math.sqrt(vec[0]*vec[0] + vec[2]*vec[2]);
  108.  
  109.         vec[0] /= d;
  110.  
  111.         vec[2] /= d;
  112.  
  113.  
  114.  
  115.         // rotation of shadow
  116.  
  117.         if (vec[0] < 0.0) {
  118.  
  119.             s_rot[3] = (float)java.lang.Math.acos(-vec[2]);
  120.  
  121.         } else {
  122.  
  123.             s_rot[3] = -(float)java.lang.Math.acos(-vec[2]);
  124.  
  125.         }
  126.  
  127.  
  128.  
  129.         // set rotation of shadow
  130.  
  131.             setRotation.setValue(s_rot);
  132.  
  133.  
  134.  
  135.         ////////////////////
  136.  
  137.         // SHADOW LENGTH
  138.  
  139.         ////////////////////
  140.  
  141.  
  142.  
  143.         // whether light height is higher than top of tree
  144.  
  145.         if (l_trans[1] < t_scale[1]) {
  146.  
  147.             s_scale[2] = 0.0f;
  148.  
  149.         } else {
  150.  
  151.             s_scale[2] = d / (l_trans[1] - t_scale[1]);
  152.  
  153.         }
  154.  
  155.  
  156.  
  157.         // set length of shadow
  158.  
  159.             setScale.setValue(s_scale);
  160.  
  161.         }
  162.  
  163.     }
  164.  
  165. }
  166.  
  167.